<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class TablasMaestra extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->form_data = new stdClass();
        if (!empty($this->session->userdata['nombre'])) {
            $this->form_data->nom = $this->session->userdata['nombre'];
            $this->form_data->ape = $this->session->userdata['apellido'];
            $this->form_data->suc = $this->session->userdata['sucursal'];
            $this->load->helper('navigation');
        } else {
            redirect("acceso");
        }
    }

    public function tablaMaestra() {
        $this->pagina('tablaMaestra_view', array());
    }

    public function listTMLogistica($tipo, $param1) {
        $this->load->model("logistica/tablaMaestra_model");
        $parametros = array($tipo, $param1);
        $data['registros'] = $this->tablaMaestra_model->list_TM_Log($parametros);
        echo json_encode($data);
    }

    public function listTMLogisticaxCod($cod) {
        $this->load->model("logistica/tablaMaestra_model");
        $parametros = array($_POST["tipo"], $cod);
        $data['registros'] = $this->tablaMaestra_model->listarLogTMxCod($parametros);
        echo json_encode($data);
    }

    public function InUp_LogTM() {
        $this->load->model("logistica/tablaMaestra_model");
        $params = array($_POST["tipo"], $_POST["cod"], $_POST["desc"], $_POST["param"]);
        try {
            $this->tablaMaestra_model->IU_TM_Log($params);
            $valor = true;
        } catch (Exception $exc) {
            $valor = false;
        }
        echo json_encode($valor);
    }

    public function proveedores() {
        $this->load->model("logistica/proveedor_model");
        $data['proveedores'] = $this->proveedor_model->list_proveedor(0);
        $this->pagina("proveedor_view", $data);
    }

    public function formProveedores($cod) {
        $this->setValProveedor($cod);
        $this->pagina("formProveedor_view", array());
    }

    public function setValProveedor($cod) {
        if ($cod == 0) {
            $this->form_data->id = $cod;
            $this->form_data->nom_pro = "";
            $this->form_data->ruc = "";
            $this->form_data->dir = "";
            $this->form_data->tel = "";
            $this->form_data->email = "";
        } else {
            $this->load->model("panel_model");
            $proveedores = $this->proveedor_model->list_proveedor($cod);
            $this->form_data->id = $proveedores{0}->id_pro;
            $this->form_data->nom_pro = $proveedores{0}->nom_pro;
            $this->form_data->ruc = $proveedores{0}->ruc_pro;
            $this->form_data->dir = $proveedores{0}->dir_pro;
            $this->form_data->tel = $proveedores{0}->tel_pro;
            $this->form_data->email = $proveedores{0}->email;
        }
    }

    public function registrarProveedor() {
        $this->load->model("logistica/proveedor_model");
        $params = array($_POST["proveedor"], $_POST["ruc"], $_POST["dir"],
            $_POST["tel"], $_POST["email"]);
        try {
            $codProv = $this->proveedor_model->IU_Proveedor($params, $_POST["idProv"]);
            $valor = true;
        } catch (Exception $exc) {
            $codProv = 0;
            $valor = false;
        }
        if (isset($_POST["tipo"])) {
            $this->setValProveedor($_POST["idProv"]);
            $this->pagina('formProveedor_view', array());
        } else {
            echo json_encode(array('cod' => $codProv, 'valor' => $valor));
        }
    }

    private function pagina($vista, $data) {
        $this->load->model("panel_model");
        $menu["logistica"] = "active open";
        $user = $this->session->userdata['codigo'];
        $params = array($user,1);
        $menu["menu"] = $this->panel_model->menu($params);
        $this->load->view('estructura/head');
        $this->load->view('estructura/madmin', $menu);
        $this->load->view('intranet/logistica/' . $vista, $data);
        $this->load->view('estructura/footer');
    }

}

?>
